package com.econ.powercloud.ems.config;

import com.econ.powercloud.ems.util.ProfileUtil;
import com.google.common.collect.Sets;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author chenxiaoqi
 * @since 2021/07/28
 */
@EnableOpenApi
@Configuration
public class Swagger2Config {

    private static final String[] ENABLE_ENV = {"dev","gkcp","test","double"};

    @Bean
    public Docket createRestApi() {
        List<RequestParameter> parameters = new ArrayList<>();
        parameters.add(new RequestParameterBuilder()
                .name("satoken")
                .description("用户信息请求头")
                .in(ParameterType.HEADER)
                .required(false)
                .build());

        String env = ProfileUtil.getActiveProfile();
        boolean enabled = Arrays.asList(ENABLE_ENV).contains(env);
        return new Docket(DocumentationType.OAS_30).pathMapping("/")
                .enable(enabled)
                .globalRequestParameters(parameters)
                .apiInfo(this.apiInfo())
                .select() // 指定需要发布到Swagger的接口目录，不支持通配符
                .apis(RequestHandlerSelectors.basePackage("com.econ.powercloud.ems.controller"))
                .paths(PathSelectors.any())
                .build().protocols(Sets.newHashSet("http"));
    }

    /**
     * 项目信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("EMS服务")
                .version("Application Version: 1.0")
                .build();
    }
}
